/**
 * 
 */
package info.niwota.frames.net;

import info.niwota.frames.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author qiangli
 *
 */
public class NetFrameUtil {
	private static final String TAG = "NetFrameUtil";
	
	private Activity mContext;

	/**
	 * 
	 */
	public NetFrameUtil(Activity context) {
		mContext = context;
	}

	public void doExport(final String frame) {
		final String title = "Export to:";
		LayoutInflater factory = LayoutInflater.from(mContext);
		final View view = factory.inflate(R.layout.export_net,
				null);
		AlertDialog d = new AlertDialog.Builder(mContext)
				.setTitle(title)
				.setView(view)
				.setPositiveButton(R.string.action_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								try {
									EditText h = (EditText) view.findViewById(R.id.export_host);
									EditText p = (EditText) view.findViewById(R.id.export_port);
									
									final String host = h.getText().toString();
									final int port = Integer.parseInt(p.getText().toString());
									
									//quick and dirty
									AlertDialog a = (AlertDialog) dialog;
									a.setTitle(R.string.text_please_wait);
									//
									final int cnt = exportData();
									
									Client.send(host, port, frame);
									
									showResult();
									
//									showResult(R.string.export_success_title, String.format(mContext
//											.getResources().getString(R.string.export_success), cnt));
//								
								} catch (Exception e) {
									showError(e);
								}
							}
						})
				.setNegativeButton(R.string.action_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}
						}).create();
		d.show();
	}

	private int exportData() {
		// TODO Auto-generated method stub
		return 0;
	}

	private void showResult() {
		// TODO Auto-generated method stub
		
	}

	private void showError(final Exception e) {
		Toast.makeText(mContext, e.getMessage()+"", Toast.LENGTH_SHORT).show();
	}
	
	private void showErrorBG(final Exception e) {
		mContext.runOnUiThread(new Runnable() {
            public void run() {
            	Toast.makeText(mContext, e.getMessage()+"", Toast.LENGTH_SHORT).show();
            }
        });
	}
	
//	private Long parseLong(String s, Long val) {
//		try {
//			return Long.parseLong(s);
//		} catch (Exception e) {
//		}
//		return val;
//	}
	
	public void doImport() throws Exception {
		final AlertDialog[] d = new AlertDialog[1];
		final Server serv = new Server(new Callback() {
			public void handle(Wrap data) {
				try {
					importData(data);

					if (d[0] != null) {
						d[0].dismiss();
					}
				} catch (Exception e) {
					e.printStackTrace();
					
					showErrorBG(e);
				}
			}
		});
		
		final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
					int which) {
				serv.cancel();
			}
		};
		
		
		serv.start();
		//
		final String title = "Import ready at:";
		final String msg = "Host: " + serv.getAddress() + " Port: " + serv.getPort();
		d[0] = new AlertDialog.Builder(mContext).setCancelable(true).setTitle(title).setMessage(msg)
				.setNegativeButton(R.string.action_cancel, listener).create();
		d[0].show();
	}

	private void importData(Wrap data) {
	// TODO Auto-generated method stub
	
	}

//	private void showResult(int titleId, String msg) {
//		new AlertDialog.Builder(mContext).setCancelable(false).setTitle(
//				mContext.getResources().getString(titleId)).setMessage(msg)
//				.setPositiveButton(R.string.action_ok,
//						new DialogInterface.OnClickListener() {
//							public void onClick(DialogInterface dialog,
//									int which) {
//							}
//						}).create().show();
//	}
	
//	private void showResultBG(final int titleId, final String msg) {
//		mContext.runOnUiThread(new Runnable() {
//            public void run() {
//            	new AlertDialog.Builder(mContext).setCancelable(false).setTitle(
//        				mContext.getResources().getString(titleId)).setMessage(msg)
//        				.setPositiveButton(R.string.action_ok,
//        						new DialogInterface.OnClickListener() {
//        							public void onClick(DialogInterface dialog,
//        									int which) {
//        							}
//        						}).create().show();
//            }
//        });
//	}
}
